﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VSM.Admin.DAO;

namespace VSM
{
    public partial class SystemGroupList : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
           /* if (Session[Utils.AppConstants.UserCode] == null
             || Session[Utils.AppConstants.CompanyName] == null
             || Session[Utils.AppConstants.CompanyCode] == null
              || Session[Utils.AppConstants.OutletCode] == null
                || Session[Utils.AppConstants.OutletName] == null
               || Session[Utils.AppConstants.IsSupperUser] == null
              || Session[Utils.AppConstants.Password] == null)
            {
                Response.Redirect("Login.aspx");
            }*/
            try
            {
                /*if (System.Configuration.ConfigurationManager.AppSettings["HoldingDatabase"].ToString() != Session[VSM.Utils.AppConstants.CompanyCode].ToString())
                {
                    Response.Redirect("Homepage.aspx");
                }*/
                if (!IsPostBack)
                {
                        LoadDealer("");
                        LoadItem(this.ddl_Dealer.SelectedValue);
                }
            }
            catch (Exception ex)
            {
                string alert = "alert('Error:" + ex.Message.Replace('\'', ' ') + " ');";
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", alert, true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvSearchResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");
                e.Row.Attributes["style"] = "cursor:pointer";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkDelete_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow gr = (GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent;
                Label lblGroupID = (Label)gr.FindControl("lblGroupCode");

                ArrayList sqlList = new ArrayList();
                DatabaseParameters KeyParameters = new DatabaseParameters();

                // Delete UserGroup
                KeyParameters.Add(new DatabaseParameter("ID_Group", lblGroupID.Text));
                SqlHelper.DeleteCommand(KeyParameters, "tbl_UsersGroups");
                sqlList.Add(SqlHelper.SQL);

                // Delete Group
                KeyParameters.Clear();
                KeyParameters.Add(new DatabaseParameter("ID_Group", lblGroupID.Text));
                SqlHelper.DeleteCommand(KeyParameters, "tbl_Groups");
                sqlList.Add(SqlHelper.SQL);

                if (!SqlHelper.ExecuteQuery(sqlList, Data.ConnectionString))
                {
                    this.lblError.Text = SqlHelper.ErrMsg;
                }
                else
                {
                    LoadItem(this.ddl_Dealer.SelectedValue);
                    this.lblError.Text = "Operation complete successful!";
                }
               
                this.lblError.Visible = true;
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvSearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.grvGroups.PageIndex = e.NewPageIndex;
            //LoadOutletSupplier(this.drdOutlet.SelectedValue.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadItem(string aID_Dealer)
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("ID_Group");
            tb.Columns.Add("ID_Dealer");
            tb.Columns.Add("Description");
            BindItem(tb, aID_Dealer);
        }
        /// <summary>
        /// 
        /// </summary>
        private void LoadDealer(string aID_Dealer)
        {
            DataSet dsDealer = null;
            try
            {
                if (string.IsNullOrEmpty(aID_Dealer))
                    dsDealer = SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.Text, "Select ID_Dealer, DealerName From tbl_Dealer ");
                else
                    dsDealer = SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.Text, "Select ID_Dealer, DealerName From tbl_Dealer Where ID_Dealer = '" + aID_Dealer + "'");

                ddl_Dealer.DataSource = dsDealer.Tables[0];
                ddl_Dealer.DataValueField = "ID_Dealer";
                ddl_Dealer.DataTextField = "DealerName";
                ddl_Dealer.DataBind();
                ddl_Dealer.Items.Insert(0, "");

                ddl_Dealer.Items.FindByValue(aID_Dealer).Selected = true;
            }
            catch (Exception ex)
            {
                string alert = "alert('Error: " + ex.Message + " ');";
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "LoadDealer", alert, true);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindItem(DataTable tb, string aID_Dealer)
        {
            try
            {
                DataSet dsGroup = null;
                if(string.IsNullOrEmpty(aID_Dealer))
                    dsGroup = SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.Text, "Select ID_Group, [Description], ID_Dealer From tbl_Groups");
                else
                    dsGroup = SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.Text, "Select ID_Group, [Description], ID_Dealer From tbl_Groups Where ID_Dealer = '" + aID_Dealer + "'");

                this.grvGroups.DataSource = dsGroup.Tables[0];
                this.grvGroups.DataBind();
            }
            catch(Exception ex)
            {
                string alert = "alert('Error: " + ex.Message + " ');";
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "BindItem", alert, true);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DeleteItem(object sender, GridViewDeleteEventArgs e)
        {
            e.Cancel = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoadComplete(EventArgs e)
        {
            try
            {
                base.OnLoadComplete(e);
                if (this.Request["__EVENTARGUMENT"] != null && this.Request["__EVENTARGUMENT"].ToString() != "")
                {
                    this.lblError.Visible = false;
                }
            }
            catch (Exception ex)
            {
                this.lblError.Visible = true;
                this.lblError.Text = ex.Message;
            }
        }

        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lnkEdit_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewRow gr = (GridViewRow)((DataControlFieldCell)((LinkButton)sender).Parent).Parent;
                Label lblGroupID = (Label)gr.FindControl("lblGroupCode");
                Response.Redirect("SystemGroupEdit.aspx?id=" + lblGroupID.Text);
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddl_Dealer_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadItem(this.ddl_Dealer.SelectedValue.ToString());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddGroup_Click(object sender, EventArgs e)
        {
            // Open new user
            Response.Redirect("SystemGroupEdit.aspx?id=");
        }
    }
}